package com.example.controller;

import com.example.entity.BaseResponse;
import com.example.entity.EquipmentEntity;
import com.example.entity.EquipmentSearchEntity;
import com.example.entity.ResultBody;
import com.example.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    @RequestMapping("/getEquipment")
    public EquipmentEntity getTask(int id) {
        return new EquipmentEntity();
    }

    /**
     * 列出所有设备信息
     *
     * @return 设备信息列表的封装对象
     */
    @RequestMapping("/listEquipment")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<EquipmentEntity> listTask() {
        List<EquipmentEntity> equipmentList = equipmentService.findAll();
        return new BaseResponse<EquipmentEntity>(0, "success",
                equipmentList.size(), equipmentList);
    }

    /**
     * 保存设备信息
     *
     * @param method 设备保存方法，可选值为"添加"或"修改"
     * @param equipmentEntity 设备信息实体类
     * @return 返回操作结果的封装对象
     */
    @RequestMapping("/saveEquipment")
    @ResponseBody
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody saveEquipment(String method,EquipmentEntity equipmentEntity){
        System.out.println(equipmentEntity.toString());
        System.out.println(method);
        if (equipmentService.saveEquipment(method,equipmentEntity)){
            return new ResultBody(0, "success");
        } else{
            return new ResultBody(1, "fail");
        }
    }

    /**
     * 删除设备信息
     *
     * @param id 设备ID
     * @return 返回操作结果的封装对象
     */
    @RequestMapping("/deleteEquipment")
    @ResponseBody
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody deleteEquipment(int id){
        if (equipmentService.deleteEquipment(id)){
            return new ResultBody(0, "success");
        } else{
            return new ResultBody(1, "fail");
        }
    }

    /**
     * 查询设备信息
     *
     * @param equipmentSearch 查询条件实体类
     * @return 设备信息列表的封装对象
     */
    @RequestMapping("/equipmentSearch")
    @ResponseBody
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<EquipmentEntity> equipmentSearch(EquipmentSearchEntity equipmentSearch) {
        System.out.println(equipmentSearch);
        equipmentSearch.setStart((equipmentSearch.getPage()-1)*equipmentSearch.getLimit());
        List<EquipmentEntity> equipmentList = equipmentService.equipmentSearch(equipmentSearch);
        return new BaseResponse<EquipmentEntity>(0, "success",
                equipmentService.equipmentSearchCount(equipmentSearch), equipmentList);
    }
}
